<?
Prado::using('System.Web.UI.ActiveControls.*'); 
Prado::using('Application.pages.utils.CommonUtils');

class EditProfile extends TPage
{

	public $ownerRecord = null;
	public $currentRecord = null;
	//public $serverPathDownload = "user_images/";
	public $id = -1;
	//public $serverPathDownload = "assets/images/";
	
	public function onInit($param)
    {	
//			parent::onInit($param);
			if(!$this->IsPostBack)  // if the page is requested the first time
			{
				
				
			}
			
			$this->id = $this->user->ID;
			
			$str = $this->btnUpload->getClientID();
			//$this->getPage()->getClientScript()->registerEndScript('', "genericMSG('$str')");
			//$this->Shout("You just entered the Edit profile");

			$this->initCurrentProfile();
			if($this->hasPermission())
			{
				$this->initEditProfile();
			}
	}
	
	public function Shout($msg)
	{
		$this->getPage()->getClientScript()->registerEndScript('', "genericMSG('$msg')");
	}
	
	public function initCurrentProfile()
	{
		$this->txtDisplayName->Text = $this->getOwnerRecord()->username;		

	}
	
	public function hasPermission()
	{
		// TO DO
		return true;
	}
	
	// for fileUploaded
	public function fileUploaded($sender,$param)
    {
		echo "<br/>".$sender->HasFile."<br/>".$sender->FileName;
        if($sender->HasFile)
        {
			$size = $sender->FileSize / 1024;
			if($size > 500)
				$this->Shout("Error : file size exceeded the limits");
			else
			{
				
				//**** at server used >>>	$path = $this->Application->AssetManager->getBasePath() ."/images/". $this->id ."/";
				//$path = $this->Application->AssetManager->getBasePath() ."\\images\\". $this->id ."\\";
				$path = $this->Application->getBasePath() . "\\..\\" . $this->Application->Parameters['ImageFolderName'] ."\\".$this->id."\\";

				// directory is not exists
				if(is_dir($path) == null)
				{
					
					mkdir($path,0777,true);
				}
					
				
				
			 	$sender->saveAs($path . $sender->FileName);
				$this->deleteOldPic();
				$this->updateAvatarDB($sender->FileName);
				$this->Response->reload();
			}
        }
		else
			$this->Shout("Error : file doesn't exists");
    }
	
	public function getCurrentRecord()
	{
		// use lazy init
	//	$userRecord = UserRecord::finder()->findByEmail($this->user->Email); $this->id
		if($this->currentRecord == null)
			$this->currentRecord = UserRecord::finder()->findByUser_Id($this->id);
		return $this->currentRecord;
	}
	
	public function deleteOldPic()
	{
		$old_path = $this->getCurrentRecord()->avatarPath;
		if($old_path != null)
		{	
			//**** at server used >>>	$path = $this->Application->AssetManager->getBasePath() ."/images/". $this->id ."/";
			//$old_path = $this->Application->AssetManager->getBasePath() ."\\images\\". $this->id ."\\". $old_path;
			
			$old_path = $this->Application->getBasePath() . "\\..\\" . $this->Application->Parameters['ImageFolderName'] ."\\".$this->id."\\".$old_path ;
			die(is_file($old_path). " path: ".$old_path);
			// check the existence of file before delete it
			if(is_file($old_path))
			{
				$flgDelete = unlink($old_path);
				if($flgDelete)
					echo "File deleted";
				else
					echo "File cannot be deleted";
			}

		}
		
	}
	
	public function updateAvatarDB($filename)
	{
		$user = $this->getOwnerRecord();
		$user->avatarPath = $filename; //change property
		$user->save();
		
	}

	public function resetAvatar()
	{
		$this->deleteOldPic();
		$this->updateAvatarDB(null);
		$this->Response->reload();
	}
	
	public function initEditProfile()
	{
		$owner = $this->getOwnerRecord();
		$this->firstName->Text = $owner->first_name;
		$this->lastName->Text = $owner->last_name;
		if($owner->sex)
			$this->radMale->Checked = true;
		else
			$this->radFemale->Checked = true;
			
		if($owner->isPrivate)
			$this->isPrivateChk->Checked = true;
		else
			$this->isPrivateChk->Checked = false;	
		/// init D M Y for select
		// Init Data
		$this->DDLYear->DataSource=$this->getYear();
		$this->DDLYear->dataBind();
		 
        $this->DDLMonth->DataSource=$this->getMonth();
        $this->DDLMonth->dataBind();
		
		$this->DDLDay->DataSource=$this->getDay();
		$this->DDLDay->dataBind();
		
//		$this->birthDate->Date = "10-10-2004";
		
		// setDay
		$date = explode("-",$owner->birthDate);
		$year = $date[0];
		$month = $date[1];
		$day = $date[2];
		
		$this->DDLYear->setSelectedIndex($this->getYearIndex($year));
		$this->DDLMonth->setSelectedIndex($month-1);
		$this->DDLDay->setSelectedIndex($day-1);
//		echo "\n>>>>> " . $year.",".$month. ",".$day."\n";
	}
	
	public function getMonth()
	{
	  $data = array('1'=>'Jan',
					'2'=>'Feb',
					'3'=>'Mar',
					'4'=>'Apr',
					'5'=>'May',
					'6'=>'Jun',
					'7'=>'July',
					'8'=>'Aug',
					'9'=>'Sep',
					'10'=>'Oct',
					'11'=>'Nov',
					'12'=>'Dec');		
		return $data;			
	}
	
	public function getDay()
	{
		$data = array();
		for($i = 1;$i <= 31; $i++)
		{
			array_push($data ,$i);
		}
		return $data;
	}
	
	public function getYear()
	{
		$data = array();
		for($i = 1950;$i <= 2005; $i++)
		{
			array_push($data ,$i);
		}
		return $data;
	}
	/**
	 * This method returns the index of param($year)
	 */
	public function getYearIndex($year)
	{
		$index = 0;
		for($i = 1950;$i <= 2005; $i++)
		{
			if($i == $year)
				return $index;
			$index++;
		}
		return 0;
	}
	
	public function getOwnerRecord()
	{
		// use lazy init
		if($this->ownerRecord == null)
			$this->ownerRecord = UserRecord::finder()->findByUser_Id($this->user->ID);
		return $this->ownerRecord;
	}
	
	public function getAvatar($user)
	{
		$img_path = $this->Application->Parameters['ImageFolderName'];
		return CommonUtils::getAvatar($img_path,$user);	
	}
}

?>